package com.newpage.module_gather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.newpage.module_gather.R;
import com.wetao.lib_common.bean.response.WrongQuestionInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * author: zhouyh
 * created on: 2020/9/16 6:19 PM
 * description: 个人中心班级适配器
 */
public class CommonQuestionNumAdapter extends RecyclerView.Adapter<CommonQuestionNumAdapter.ClassViewHolder> {


    private Context mContext;
    private List<WrongQuestionInfo.Data> data = new ArrayList<>();//作业列表
    private int checkItem = 0;

    public void notifyDataChanged() {
        notifyDataSetChanged();
    }


    //设置试题
    public void setData(List<WrongQuestionInfo.Data> datas) {
        if (datas != null) {
            this.data.clear();
            this.data.addAll(datas);
            notifyDataChanged();
        }
    }

    public CommonQuestionNumAdapter(Context context) {
        mContext = context;
    }

    @Override
    public ClassViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.gather_question_screen_num_item, parent, false);
        return new ClassViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull ClassViewHolder classViewHolder, final int position) {
        classViewHolder.tv_question_screen_no.setText(String.format("%s",position+1));
        if (getCheckItem() == position){
            classViewHolder.tv_question_screen_no.setSelected(true);
        }else {
            classViewHolder.tv_question_screen_no.setSelected(false);
        }
        classViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getCheckItem() == position){
                    return;
                }
                if (clickListener!=null){
                    setCheckItem(position);
                    clickListener.onItemClick(position);
                }
            }
        });

    }





    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }


    public int getCheckItem() {
        return checkItem;
    }

    public void setCheckItem(int checkItem) {
        this.checkItem = checkItem;
        notifyDataChanged();
    }


    //试题列表
    class ClassViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout ll_screen_num_item;
        private TextView tv_question_screen_no;


        public ClassViewHolder(View itemView) {
            super(itemView);

            tv_question_screen_no = itemView.findViewById(R.id.tv_question_screen_no);
            ll_screen_num_item = itemView.findViewById(R.id.ll_screen_num_item);

        }
    }


    private OnItemClickListener clickListener;
    public OnItemClickListener getOnUserClassItemClick() {
        return clickListener;
    }
    public void setOnQuestionNumItemClick(OnItemClickListener listener) {
        this.clickListener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

}


